package com.brainstrongtech.bfs.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.brainstrongtech.bfs.domian.User;
import com.brainstrongtech.bfs.domian.UserConfigParam;
import com.brainstrongtech.bfs.service.UserService;
import com.brainstrongtech.bfs.util.Message;

/**
 * 
 * @Description:
 * @author: tianhua.xie
 * @Date: 2017年4月3日 下午9:39:15
 *
 */
@Controller
@RequestMapping("/userPersonalController")
public class UserPersonalController extends BaseController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/preModifyPersonalInfo")
	public String preModifyPersonalInfo(ModelMap modelMap, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("loginedUser");
		user.setUserPwd("");
		modelMap.put("user", user);
		return "user_personal/user_personal_info_edit";
	}

	@RequestMapping(value = "/modifyPersonalInfo")
	@ResponseBody
	public Message modifyPersonalInfo(User user, HttpServletRequest request) throws Exception {

		Message message = null;

		try {
			// 修改个人信息
			user = this.userService.modifyPersonalInfo(user);
			request.getSession().setAttribute("loginedUser", user);
			// 封装成功信息为message对象
			message = coverMessage("200", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			// 封装失败信息为message对象
			message = coverMessage("500", e.getMessage());
		}

		return message;
	}


	@RequestMapping(value = "/preModifyPersonalPwd")
	public String preModifyPersonalPwd(ModelMap modelMap) {

		return "user_personal/user_personal_pwd_edit";
	}

	@RequestMapping(value = "/modifyPersonalPwd")
	@ResponseBody
	public Message modifyPersonalPwd(String newPwd, String oldPwd, HttpServletRequest request) throws Exception {

		Message message = null;
		User user = (User) request.getSession().getAttribute("loginedUser");

		try {

			this.userService.modifyPersonalPwd(user.getUserId(), newPwd, oldPwd);
			request.getSession().removeAttribute("loginedUser");
			message = coverMessage("200", "修改成功");

		} catch (Exception e) {
			e.printStackTrace();
			message = coverMessage("500", e.getMessage());
		}

		return message;
	}

	@RequestMapping(value = "/preModifyConfigParam")
	public String preModifyConfigParam(ModelMap modelMap, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("loginedUser");
		UserConfigParam param = userService.loadUserConfigParamById(user.getUserId());
		modelMap.put("configParam", param);
		return "user_personal/user_config_param_edit";
	}

	@RequestMapping(value = "/modifyConfigParam")
	@ResponseBody
	public Message modifyConfigParam(UserConfigParam param, HttpServletRequest request) throws Exception {

		Message message = null;
		User user = (User) request.getSession().getAttribute("loginedUser");
		param.setParamId(user.getUserId());
		try {

			this.userService.modifyConfigParam(param);
			request.getSession().setAttribute("configParam", param);
			message = coverMessage("200", "修改成功");

		} catch (Exception e) {
			e.printStackTrace();
			message = coverMessage("500", e.getMessage());
		}

		return message;
	}



}
